package com.paic.mhis.hcpms.common.community.biz.service.impl;

import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.alibaba.fastjson.JSONObject;
import com.paic.mhis.common.base.dto.BaseDTO;
import com.paic.mhis.common.base.intergration.dao.CommonDAO;
import com.paic.mhis.common.util.BusinessServiceUtil;
import com.paic.mhis.common.util.Constants;
import com.paic.mhis.core.util.BaseConstants;
import com.paic.mhis.hcpms.common.community.biz.service.BaseService;
import com.paic.pafa.app.biz.service.BusinessServiceException;

@Component("baseService")
public class BaseServiceImpl implements BaseService {

	/**
	 * 根据codetype查询基表对应的数据
	 */
	@Autowired
	private CommonDAO commonDao;
	private static Map<String, String> methodMap;

	static {
		methodMap = new HashMap<String, String>();
		methodMap.put("hospital_level", "getBaseCode");
		methodMap.put("classification_manage", "getBaseCode");
		methodMap.put("social_insurance_type", "getBaseCode");
		methodMap.put("glb_bgt_ratio_type", "getBaseCode");
		methodMap.put("tour_task_sts", "getBaseCode");
		methodMap.put("diag_type", "getBaseCode");
		methodMap.put("item_type", "getBaseCode");
		methodMap.put("payment_mode", "getBaseCode");
		methodMap.put("id_type", "getBaseCode");
		methodMap.put("job_type", "getBaseCode");

		methodMap.put("get_all_code", "getAllCode");
		methodMap.put("bse_hcpms_org_type", "getOrgType");
		methodMap.put("bse_hcpms_economic_type", "getEconomicType");
		methodMap.put("hcpms_tour_inspect_item", "getInspectItem");
		methodMap.put("hcpms_tour_detail", "getTourDetail");
		methodMap.put("hcpms_health_provider", "getHealthProvider");
		methodMap.put("bse_hcpms_department", "getHcpmsDepartment");
		methodMap.put("bse_hcpms_region", "getRegion");
		methodMap.put("agreement_areas", "getAgreementArea");
		methodMap.put("diagnosis_treat", "getDiagnosisTreat");

		methodMap.put("agreement_disease", "getDisease");
		methodMap.put("diagnosis_treat2", "getDiagnosisTreatDetail");
		methodMap.put("hcpms_health_provider_list", "gethealthProviderList");
		methodMap.put("keyDePartment", "getKeyDePartment");
		methodMap.put("operator_list", "getOperatorList");
		methodMap.put("pointType", "getBaseCode");
		methodMap.put("medicalPro", "getBaseCode");
		methodMap.put("cityDep", "getBaseCodeList");
		methodMap.put("provincialDep", "getBaseCodeList");
		methodMap.put("personTypeHos", "getBaseCode");
		methodMap.put("personTypeShop", "getBaseCode");
		methodMap.put("sex", "getBaseCodeList");
		methodMap.put("qualifiedLev", "getBaseCode");
		methodMap.put("openBusiness", "getBaseCode");
		methodMap.put("agreementType", "getBaseCode");
		methodMap.put("workScope", "getBaseCodeList");

		methodMap.put("audit_target_type", "getBaseCodeList");
		methodMap.put("audit_source_type", "getBaseCodeList");
		methodMap.put("audit_complaint_type", "getBaseCodeList");
		methodMap.put("audit_first_trial", "getBaseCodeList");
		methodMap.put("audit_case_status", "getBaseCodeList");
		methodMap.put("audit_case_approve", "getBaseCodeList");
		methodMap.put("code_list", "getBaseCodeList");
	}

	@Override
	public Map<String, Object> getCode(JSONObject transferJson) throws BusinessServiceException {
		BaseDTO infoDTO;
		Map<String, Object> result = null;
		try {
			infoDTO = JSONObject.toJavaObject(transferJson, BaseDTO.class);
			// 根据前端传入的编码类型编号，在配置文件中找到对应的编码类型；
			String codeType = getCodeNo(infoDTO.getCodeType());
			transferJson.put("codeType", codeType);

			result = execute(codeType, transferJson);
		} catch (Exception e) {
			BusinessServiceUtil.processException(e);
		}
		return result;
	}

	private String getCodeNo(String codeType) {
		if ("orgType".equals(codeType)) {
			return "bse_hcpms_org_type";
		} else if ("socialInsuranceType".equals(codeType)) {
			return "social_insurance_type";
		} else if ("orgLevel".equals(codeType)) {
			return "hospital_level";
		} else if ("classificationType".equals(codeType)) {
			return "classification_manage";
		} else if ("economicType".equals(codeType)) {
			return "bse_hcpms_economic_type";
		} else if ("areas".equals(codeType)) {
			return "agreement_areas";
		} else if ("region".equals(codeType)) {
			return "bse_hcpms_region";
		} else if ("pointType".equals(codeType)) {
			return "pointType";
		} else if ("medicalPro".equals(codeType)) {
			return "medicalPro";
		} else if ("provincialDep".equals(codeType)) {
			return "provincialDep";
		} else if ("cityDep".equals(codeType)) {
			return "cityDep";
		} else if ("personTypeHos".equals(codeType)) {
			return "personTypeHos";
		} else if ("personTypeShop".equals(codeType)) {
			return "personTypeShop";
		} else if ("dePartment".equals(codeType)) {
			return "bse_hcpms_department";
		} else if ("sex".equals(codeType)) {
			return "sex";
		} else if ("jobType".equals(codeType)) {
			return "job_type";
		} else if ("regionXiamen".equals(codeType)) {
			return "bse_hcpms_region";
		} else if ("id_type".equals(codeType)) {
			return "id_type";
		} else if ("qualifiedLev".equals(codeType)) {
			return "qualifiedLev";
		} else if ("openBusiness".equals(codeType)) {
			return "openBusiness";
		} else if ("agreementType".equals(codeType)) {
			return "agreementType";
		}else if ("workScope".equals(codeType)) {
			return "workScope";
		}else if ("auditTargetType".equals(codeType)) {
			return "audit_target_type";
		}else if ("auditSourceType".equals(codeType)) {
			return "audit_source_type";
		}else if ("auditComplaintType".equals(codeType)) {
			return "audit_complaint_type";
		}else if ("auditFirstTrial".equals(codeType)) {
			return "audit_first_trial";
		}else if ("auditCaseStatus".equals(codeType)) {
			return "audit_case_status";
		}else if ("auditCaseApprove".equals(codeType)) {
			return "audit_case_approve";
		}else{
			return "code_list";
		}
		
	}

	private Map<String, Object> execute(String codeType, JSONObject transferJson) throws Exception {
		String methodName = methodMap.get(codeType);
		Method[] methods = this.getClass().getDeclaredMethods();
		Map<String, Object> result = null;
		for (Method tmpMethod : methods) {
			if (tmpMethod.getName().equals(methodName)) {
				if (!"getKeyDePartment".equals(methodName)) {
					tmpMethod.setAccessible(true);
					result = (Map<String, Object>) tmpMethod.invoke(this, new Object[] { transferJson });
				} else {
					tmpMethod.setAccessible(true);
					result = (Map<String, Object>) tmpMethod.invoke(this);
				}
			}
		}

		if (result == null) {
			result = new HashMap<String, Object>();
			result.put(Constants.SUCCESS_RESULT_KEY, "");
			generateSuccessResult(result, Constants.SUCCESS_STATE_VALUE, Constants.SELECT_SUCCESS);
		}

		return result;
	}

	private void generateSuccessResult(Map<String, Object> result, String stateValue, String msgValue) {
		result.put(Constants.STATE_KEY, stateValue);
		result.put(Constants.MSG_KEY, msgValue);
	}

	/**
	 * 查询 • 卫生机构类别
	 * 
	 * @param orgType
	 * @param codeno
	 */
	public Map<String, Object> getOrgType(JSONObject transferJson) throws BusinessServiceException {
		String orgType = transferJson.getString("orgTypeCode");
		String codeno = transferJson.getString("codeNo");
		List<BaseDTO> list;
		Map<String, Object> result = new HashMap<String, Object>();
		try {
			Map<String, Object> paramsMap = new HashMap<String, Object>();
			// 根据前端传入的编码类型编号，在配置文件中找到对应的编码类型；
			paramsMap.put("orgType", orgType);
			paramsMap.put("codeNo", codeno);
			list = commonDao.query("base.orgType", paramsMap);
			result.put(Constants.SUCCESS_RESULT_KEY, list);
			generateSuccessResult(result, Constants.SUCCESS_STATE_VALUE, Constants.SELECT_SUCCESS);
		} catch (Exception e) {
			BusinessServiceUtil.processException(e);
		}
		return result;
	}

	/**
	 * 根据codetype查询基表对应的数据
	 */
	private Map<String, Object> getBaseCodeList(JSONObject transferJson) throws BusinessServiceException {
		String codeno = transferJson.getString("codeNo");
		String codetype = transferJson.getString("codeType");
		List<BaseDTO> list;
		Map<String, Object> result = new HashMap<String, Object>();
		try {
			Map<String, Object> paramsMap = new HashMap<String, Object>();
			// 根据前端传入的编码类型编号，在配置文件中找到对应的编码类型；
			paramsMap.put("codeType", codetype);
			paramsMap.put("codeNo", codeno);

			list = commonDao.query("base.codeList", paramsMap);
			result.put(Constants.SUCCESS_RESULT_KEY, list);
			generateSuccessResult(result, Constants.SUCCESS_STATE_VALUE, Constants.SELECT_SUCCESS);
		} catch (Exception e) {
			BusinessServiceUtil.processException(e);
		}
		return result;
	}

	/**
	 * 根据codetype查询基表对应的数据
	 */
	@Override
	public List<BaseDTO> getBaseCodes(String codetype, String codeno) throws BusinessServiceException {
		List<BaseDTO> list = null;
		try {
			Map<String, Object> paramsMap = new HashMap<String, Object>();
			// 根据前端传入的编码类型编号，在配置文件中找到对应的编码类型；
			paramsMap.put("codeType", codetype);
			paramsMap.put("codeNo", codeno);

			list = commonDao.query("base.codeTypes", paramsMap);
		} catch (Exception e) {
			BusinessServiceUtil.processException(e);
		}
		return list;
	}

	private Map<String, Object> getBaseCode(JSONObject transferJson) throws BusinessServiceException {
		String codeno = transferJson.getString("codeNo");
		String codetype = transferJson.getString("codeType");
		List<BaseDTO> list;
		Map<String, Object> result = new HashMap<String, Object>();
		try {
			Map<String, Object> paramsMap = new HashMap<String, Object>();
			// 根据前端传入的编码类型编号，在配置文件中找到对应的编码类型；
			paramsMap.put("codeType", codetype);
			paramsMap.put("codeNo", codeno);

			list = commonDao.query("base.codeTypes", paramsMap);
			result.put(Constants.SUCCESS_RESULT_KEY, list);
			generateSuccessResult(result, Constants.SUCCESS_STATE_VALUE, Constants.SELECT_SUCCESS);
		} catch (Exception e) {
			BusinessServiceUtil.processException(e);
		}
		return result;
	}

	public Map<String, Object> getAllCode(JSONObject transferJson) throws BusinessServiceException {
		Map<String, Object> result = new HashMap<String, Object>();
		Map<String, Object> results = new HashMap<String, Object>();
		try {
			// 根据前端传入的编码类型编号，在配置文件中找到对应的编码类型；
			result.put("hosLevelType", getBaseCodes("hospital_level", ""));
			result.put("classificationType", getBaseCodes("classification_manage", ""));
			result.put("socialInsuranceType", getBaseCodes("social_insurance_type", ""));
			result.put("ratioType", getBaseCodes("glb_bgt_ratio_type", ""));
			result.put("tourTaskSts", getBaseCodes("tour_task_sts", ""));
			result.put("diagType", getBaseCodes("diag_type", ""));
			result.put("itemTypes", getBaseCodes("item_type", ""));
			result.put("paymentMode", getBaseCodes("payment_mode", ""));
			result.put("idType", getBaseCodes("id_type", ""));
			result.put("jobType", getBaseCodes("job_type", ""));

			results.put(Constants.SUCCESS_RESULT_KEY, result);
			generateSuccessResult(results, Constants.SUCCESS_STATE_VALUE, Constants.SELECT_SUCCESS);
		} catch (Exception e) {
			BusinessServiceUtil.processException(e);
		}
		return results;
	}

	/**
	 * 根据codetype查询基表对应的数据
	 */

	private Map<String, Object> getKeyDePartment() throws BusinessServiceException {
		List<HashMap<String, Object>> resultList = null;
		Map<String, Object> result = new HashMap<String, Object>();
		List<HashMap<String, Object>> resultList2 = new ArrayList<HashMap<String, Object>>();
		try {
			Map<String, Object> paramsMap = new HashMap<String, Object>();
			Map<String, Object> paramsMap2 = new HashMap<String, Object>();
			// 查询2级科室的所有业务科
			paramsMap.put("departmentType", BaseConstants.BASE_DEPARTMENTTYPE);
			paramsMap.put("codeNo", BaseConstants.BASE_CODENO);
			resultList = commonDao.query("base.department", paramsMap);
			// 查询所有2级科室下面的3级科室
			for (int i = 0; i < resultList.size(); i++) {
				String codeNo = (String) ((Map) resultList.get(i)).get("codeno");
				String codeName = (String) ((Map) resultList.get(i)).get("codename");
				Map<String, Object> tempMap = new HashMap<String, Object>();
				List tempList3 = null;
				paramsMap2.put("codeNo", codeNo);
				tempList3 = commonDao.query("base.department", paramsMap2);
				tempMap.put("codeno", codeNo);
				tempMap.put("codename", codeName);
				tempMap.put("sub", tempList3);
				resultList2.add((HashMap<String, Object>) tempMap);
			}
			result.put(Constants.SUCCESS_RESULT_KEY, resultList2);
			generateSuccessResult(result, Constants.SUCCESS_STATE_VALUE, Constants.SELECT_SUCCESS);
		} catch (Exception e) {
			BusinessServiceUtil.processException(e);
		}
		return result;
	}

	private Map<String, Object> getDiagnosisTreatDetail(JSONObject transferJson) throws BusinessServiceException {
		String codeno = transferJson.getString("codeNo");
		Map<String, Object> resultMap = null;
		Map<String, Object> result = new HashMap<String, Object>();
		try {
			Map<String, Object> paramsMap = new HashMap<String, Object>();
			// 根据前端传入的编码类型编号，在配置文件中找到对应的编码类型；
			paramsMap.put("codeNo", codeno);
			resultMap = (Map<String, Object>) commonDao.get("base.getDiagnosisTreatDetail", paramsMap);
			result.put(Constants.SUCCESS_RESULT_KEY, resultMap);
			generateSuccessResult(result, Constants.SUCCESS_STATE_VALUE, Constants.SELECT_SUCCESS);
		} catch (Exception e) {
			BusinessServiceUtil.processException(e);
		}
		return result;
	}

	private Map<String, Object> getRegion(JSONObject transferJson) throws BusinessServiceException {
		String regionType = transferJson.getString("regionType");
		String codeno = transferJson.getString("codeNo");
		List<BaseDTO> list;
		Map<String, Object> result = new HashMap<String, Object>();
		try {
			Map<String, Object> paramsMap = new HashMap<String, Object>();
			// 根据前端传入的编码类型编号，在配置文件中找到对应的编码类型；
			paramsMap.put("codeNo", codeno);
			paramsMap.put("regionType", regionType);
			list = commonDao.query("base.getRegion", paramsMap);
			result.put(Constants.SUCCESS_RESULT_KEY, list);
			generateSuccessResult(result, Constants.SUCCESS_STATE_VALUE, Constants.SELECT_SUCCESS);
		} catch (Exception e) {
			BusinessServiceUtil.processException(e);
		}
		return result;
	}

	/**
	 * 查询 • 卫生机构类别
	 * 
	 * @param orgType
	 * @param codeno
	 */

	/**
	 * 查询 • 经济类型
	 */
	private Map<String, Object> getEconomicType(JSONObject transferJson) throws BusinessServiceException {
		List<BaseDTO> list;
		Map<String, Object> result = new HashMap<String, Object>();
		try {
			list = commonDao.query("base.economicType", null);
			result.put(Constants.SUCCESS_RESULT_KEY, list);
			generateSuccessResult(result, Constants.SUCCESS_STATE_VALUE, Constants.SELECT_SUCCESS);
		} catch (Exception e) {
			BusinessServiceUtil.processException(e);
		}
		return result;
	}

	/**
	 * 查询 • 巡查项
	 */
	private Map<String, Object> getInspectItem(JSONObject transferJson) throws BusinessServiceException {
		List<BaseDTO> list;
		Map<String, Object> result = new HashMap<String, Object>();
		try {
			list = commonDao.query("base.inspectItem", null);
			result.put(Constants.SUCCESS_RESULT_KEY, list);
			generateSuccessResult(result, Constants.SUCCESS_STATE_VALUE, Constants.SELECT_SUCCESS);
		} catch (Exception e) {
			BusinessServiceUtil.processException(e);
		}
		return result;
	}

	/**
	 * 查询 医疗机构名称
	 */
	private Map<String, Object> getHealthProvider(JSONObject transferJson) throws BusinessServiceException {
		List<BaseDTO> list;
		Map<String, Object> result = new HashMap<String, Object>();
		try {
			list = commonDao.query("base.healthProvider", null);
			result.put(Constants.SUCCESS_RESULT_KEY, list);
			generateSuccessResult(result, Constants.SUCCESS_STATE_VALUE, Constants.SELECT_SUCCESS);
		} catch (Exception e) {
			BusinessServiceUtil.processException(e);
		}
		return result;
	}

	/**
	 * 根据诊疗项目、诊疗项目编号查询诊疗项目、诊疗项目编号
	 */
	public Map<String, Object> getDiagnosisTreat(JSONObject transferJson) throws BusinessServiceException {
		String codename = transferJson.getString("codeName");
		String codeno = transferJson.getString("codeNo");
		List<BaseDTO> list;
		Map<String, Object> result = new HashMap<String, Object>();
		try {
			Map<String, Object> paramsMap = new HashMap<String, Object>();
			// 判断诊疗项目名称及编号是否都为空：
			if ((null != codename && !"".equals(codename)) || (null != codeno && !"".equals(codeno))) {
				paramsMap.put("codeName", codename);
				paramsMap.put("codeNo", codeno);

				list = commonDao.query("base.diagnosisTreat", paramsMap);
				result.put(Constants.SUCCESS_RESULT_KEY, list);
				generateSuccessResult(result, Constants.SUCCESS_STATE_VALUE, Constants.SELECT_SUCCESS);
			}
		} catch (Exception e) {
			BusinessServiceUtil.processException(e);
		}
		return result;
	}

	/**
	 * 根据病种名称、病种编码，查询病种名称、病种编码
	 */
	private Map<String, Object> getDisease(JSONObject transferJson) throws BusinessServiceException {
		String codename = transferJson.getString("codeName");
		String codeno = transferJson.getString("codeNo");
		List<BaseDTO> list;
		Map<String, Object> result = new HashMap<String, Object>();
		try {
			Map<String, Object> paramsMap = new HashMap<String, Object>();
			// 判断诊疗项目名称及编号是否都为空：
			if ((null != codename && !"".equals(codename)) || (null != codeno && !"".equals(codeno))) {
				paramsMap.put("codeName", codename);
				paramsMap.put("codeNo", codeno);

				list = commonDao.query("base.disease", paramsMap);
				result.put(Constants.SUCCESS_RESULT_KEY, list);
				generateSuccessResult(result, Constants.SUCCESS_STATE_VALUE, Constants.SELECT_SUCCESS);
			}
		} catch (Exception e) {
			BusinessServiceUtil.processException(e);
		}
		return result;
	}

	/**
	 * 根据科室类型、科室编码，查询科室名称、科室编码
	 */
	private Map<String, Object> getHcpmsDepartment(JSONObject transferJson) throws BusinessServiceException {
		String departmentType = transferJson.getString("departmentType");
		String codeno = transferJson.getString("codeNo");
		List<BaseDTO> list;
		Map<String, Object> result = new HashMap<String, Object>();
		try {
			Map<String, Object> paramsMap = new HashMap<String, Object>();
			// 根据前端传入的编码类型编号，在配置文件中找到对应的编码类型；
			paramsMap.put("departmentType", departmentType);
			paramsMap.put("codeNo", codeno);

			list = commonDao.query("base.department", paramsMap);
			result.put(Constants.SUCCESS_RESULT_KEY, list);
			generateSuccessResult(result, Constants.SUCCESS_STATE_VALUE, Constants.SELECT_SUCCESS);
		} catch (Exception e) {
			BusinessServiceUtil.processException(e);
		}
		return result;
	}

	/**
	 * 医疗机构名称预收索
	 */
	private Map<String, Object> gethealthProviderList(JSONObject transferJson) throws BusinessServiceException {
		// 查询按总额付费
		String healthcareProviderName = transferJson.getString("codeName");
		List<BaseDTO> list;
		Map<String, Object> paramsMap = new HashMap<String, Object>();
		Map<String, Object> result = new HashMap<String, Object>();
		try {
			if (null != healthcareProviderName && !"".equals(healthcareProviderName)) {
				paramsMap.put("healthcareProviderName", "%" + healthcareProviderName + "%");
				list = commonDao.query("base.providerName", paramsMap);

				result.put(Constants.SUCCESS_RESULT_KEY, list);
				generateSuccessResult(result, Constants.SUCCESS_STATE_VALUE, Constants.SELECT_SUCCESS);
			}
		} catch (Exception e) {
			BusinessServiceUtil.processException(e);
		}
		return result;
	}

	/**
	 * 根据用户名查询对应的统筹区域
	 */
	public Map<String, Object> getAgreementArea(JSONObject transferJson) throws BusinessServiceException {
		String currentUser = transferJson.getString("currentUser");
		List<BaseDTO> list;
		StringBuffer areas = new StringBuffer();
		Map<String, Object> result = new HashMap<String, Object>();
		Map<String, Object> paramsMap = new HashMap<String, Object>();
		try {
			String code = transferJson.getString("areaCode");
			// area = um2Service.getPrincipalsRegionByID(currentUser);
			if (!"".equals(code)) {
				paramsMap.put("code", code);
				// 根据统筹区编码查询对应的统筹区名称及编码
				list = commonDao.query("base.agreementAreaInfo", paramsMap);
				result.put(Constants.SUCCESS_RESULT_KEY, list);
				generateSuccessResult(result, Constants.SUCCESS_STATE_VALUE, Constants.SELECT_SUCCESS);
			}
		} catch (Exception e) {
			BusinessServiceUtil.processException(e);
		}
		return result;
	}

}
